package mate.business
{
	import spark.components.NavigatorContent;

	public class PinkPonyManager
	{
		import classespinkpony.*;
		
		import mx.collections.ArrayCollection;
		import mx.controls.Alert;
		import mx.rpc.Fault;
		
		private var _practice:Practice;
		private var _country:Country;
		
		private var _blogs:ArrayCollection = new ArrayCollection();
		private var _practices:ArrayCollection = new ArrayCollection();
		private var _districts:ArrayCollection = new ArrayCollection();
		private var _locations:ArrayCollection = new ArrayCollection();
		private var _salutations:ArrayCollection = new ArrayCollection();
		private var _countries:ArrayCollection = new ArrayCollection();
		private var _postalcodes:ArrayCollection = new ArrayCollection();
		private var _breadcrumbs:ArrayCollection = new ArrayCollection();
		private var _professions:ArrayCollection = new ArrayCollection();
		private var _lat:Number = 0;
		private var _lng:Number = 0;
		private var _visitorAddress:Boolean=false;
		private var _visitorAddressText:String;

		[Bindable]
		public function get blogs():ArrayCollection
		{
			return _blogs;
		}

		public function set blogs(value:ArrayCollection):void
		{
			_blogs = value;
		}

		[Bindable]
		public function get practices():ArrayCollection
		{
			return _practices;
		}
		
		public function set practices(value:ArrayCollection):void
		{
			_practices = value;
		}
		
		[Bindable]
		public function get country():Country
		{
			return _country;
		}
		
		public function set country(value:Country):void
		{
			_country = value;
		}
		
		[Bindable]
		public function get districts():ArrayCollection{
			return this._districts;
		}
		
		public function set districts(districts:ArrayCollection):void{
			this._districts = districts;
		}
		
		[Bindable]
		public function get locations():ArrayCollection{
			return this._locations;
		}
		
		public function set locations(locations:ArrayCollection):void{
			this._locations = locations;
		}
		
		[Bindable]
		public function get practice():Practice
		{
			return _practice;
		}
		
		public function set practice(value:Practice):void
		{
			_practice = value;
		}
		
		[Bindable]
		public function get salutations():ArrayCollection
		{
			return _salutations;
		}
		
		public function set salutations(value:ArrayCollection):void
		{
			_salutations = value;
		}
		
		[Bindable]
		public function get countries():ArrayCollection
		{
			return _countries;
		}
		
		public function set countries(value:ArrayCollection):void
		{
			_countries = value;
		}
		
		[Bindable]
		public function get postalcodes():ArrayCollection
		{
			return _postalcodes;
		}
		
		public function set postalcodes(value:ArrayCollection):void
		{
			_postalcodes = value;
		}
		
		[Bindable]
		public var selectedView: int = 0;
		
		[Bindable]
		public function get breadcrumbs():ArrayCollection
		{
			return _breadcrumbs;
		}
		
		public function set breadcrumbs(value:ArrayCollection):void
		{
			_breadcrumbs = value;
		}
		
		[Bindable]
		public function set lat(lat:Number):void{
			this._lat = lat;
			visitorAddress = true;
		}
		public function get lat():Number{
			return this._lat;
		}
		[Bindable]
		public function set lng(lng:Number):void{
			this._lng = lng;
		}
		public function get lng():Number{
			return this._lng;
		}
		[Bindable]
		public function set visitorAddress(visitorAddress:Boolean):void{
			this._visitorAddress = visitorAddress;
		}
		public function get visitorAddress():Boolean{
			return this._visitorAddress;
		}
		[Bindable]
		public function set visitorAddressText(visitorAddressText:String):void{
			this._visitorAddressText = visitorAddressText;
		}
		public function get visitorAddressText():String{
			return this._visitorAddressText;
		}
		
		public function storePractice(practice:Object):void
		{
			if(practice != null){
				for each (var pract:Practice in practice)
				{
					this.practice = pract;
				}
			}
		}
		
		public function storeCountry(country:Object):void
		{
			if(country != null){
				for each (var c:Country in country)
				{
					this.country = c;
				}
			}
		}
		
		public function storeBlogs(blogs:Object):void{
			var temp: ArrayCollection = new ArrayCollection();
			
			if(blogs != null){
				for each (var blog: Blog in blogs){
					temp.addItem(blog);
				}
			}
			
			this.blogs = temp;
		}
		
		
		public function storePractices(practices:Object):void
		{
			this.practices = new ArrayCollection();
			
			if(practices != null){
				for each (var practice:Practice in practices)
				{
					this.practices.addItem(practice);
				}
			}
		}
		
		public function storeDistricts(districts:Object):void
		{
			var meh: ArrayCollection = new ArrayCollection();
			
			if(districts != null){
				for each (var district:District in districts)
				{
					meh.addItem(district);
				}
			}
			
			this.districts = meh;
		}
		
		public function storeLocations(locations:Object):void
		{
			this.locations = new ArrayCollection();
			
			if(locations != null){
				for each (var location:Location in locations)
				{
					this.locations.addItem(location);
				}
			}
		}
		
		public function storeSalutations(salutations:Object):void
		{
			this.salutations = new ArrayCollection();
			
			if(salutations != null){
				for each (var salutation:Salutation in salutations)
				{
					this.salutations.addItem(salutation);
				}
			}
		}
		
		public function storeCountries(countries:Object):void
		{
			this.countries = new ArrayCollection();
			
			if(countries != null){
				for each (var country:Country in countries)
				{
					this.countries.addItem(country);
				}
			}
		}
		
		public function storeProfessions(professions:Object):void
		{
			var temp:ArrayCollection = new ArrayCollection();
			
			if(professions != null){
				for each (var prof:Profession in professions)
				{
					temp.addItem(prof);
				}
			}
			
			this.professions = temp;
		}
		
		protected function getName(id:Number):String{
			switch (id){
				case 0: return PinkPonyMATE.VIEWHOME_LABEL;
				case 1: return PinkPonyMATE.VIEWLOCATIONS_LABEL;
				case 2: return PinkPonyMATE.VIEWLIST_LABEL;
				case 3: return PinkPonyMATE.VIEWPROFILE_LABEL;
				case 4: return PinkPonyMATE.VIEWINFO_LABEL;
				case 5: return PinkPonyMATE.VIEWREGISTRATION_LABEL;
				case 6: return PinkPonyMATE.VIEWCONTROLPANEL_LABEL;
				case 7: return PinkPonyMATE.VIEWCMS_LABEL;
				default: return "";
			}
		}
		
		public function addBreadcrumb(id:Number):void
		{
			if(this.breadcrumbs == null)
				this.breadcrumbs = new ArrayCollection();
			if(breadcrumbs.length==0 || breadcrumbs[breadcrumbs.length-1].id!=id) 
			//telkens hetzelfde event oproepen zou anders zorgen dat een broodkruimeltje blijft toegevoegd worden.
			{
				var contains:Boolean = false;
				var index:Number = -1;
				for(var i:Number = 0; i< breadcrumbs.length;i++)
				{
					if(breadcrumbs[i].id==id)
					{
						contains = true;
						index=i;
					}
				}
				if(!contains)
				{
					var breadcrumb:Breadcrumb = new Breadcrumb(id, getName(id));
					this.breadcrumbs.addItem(breadcrumb);
				}
				else
					deleteBreadcrumbs(index);
			}
			
		}
		
		public function deleteBreadcrumbs(index:Number):void{
			if(this.breadcrumbs == null)
				this.breadcrumbs = new ArrayCollection();
			
			for(var i:Number=breadcrumbs.length-1;i>index;i--)
				breadcrumbs.removeItemAt(i);
		}
		
		public function handleFault(fault:Fault):void{
			Alert.show(fault.faultString, fault.faultCode.toString());
		}
		
		public function calcDistances():void{
			for each (var practice:Object in practices)
			{
				(practice as Practice).calcDistance(lat,lng);
			}
		}	

		[Bindable]
		public function get professions():ArrayCollection
		{
			return _professions;
		}

		public function set professions(value:ArrayCollection):void
		{
			_professions = value;
		}


	}
}